﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EBA.IoC;
using System.Globalization;
using System.Threading;

namespace EBA.Localization
{
    /// <summary>
    /// 本地化
    /// </summary>
    public static class LocalizationProviders
    {
        public static ILocalizationProvider Provider = new EmptyLocalizationProvider();

        /// <summary>
        /// Translate
        /// </summary>
        /// <param name="key"></param>
        /// <param name="culture"></param>
        /// <param name="defaultText"></param>
        /// <returns></returns>
        public static string Translate(string key, CultureInfo culture, string defaultText)
        {
            var provider = Provider;

            return provider.Translate(key, culture, defaultText);
        }

        /// <summary>
        /// the shortcus for <see cref="Translate"/>
        /// </summary>
        /// <returns></returns>
        public static string T(this string key, params object[] args)
        {
            var text = Translate(key, Thread.CurrentThread.CurrentUICulture,null);

            return FormatContent(args, text);
        }

        /// <summary>
        /// the shortcus for <see cref="Translate"/>
        /// </summary>
        /// <returns></returns>
        public static string T(this string key, CultureInfo culture, params object[] args)
        {
            var text = Translate(key, culture,null);

            return FormatContent(args, text);
        }

        /// <summary>
        /// the shortcus for <see cref="Translate"/>
        /// </summary>
        /// <returns></returns>
        public static string M(this string key, string defaultText, params object[] args)
        {
            var text = Translate(key, Thread.CurrentThread.CurrentUICulture, defaultText);

            return FormatContent(args, text);
        }

        /// <summary>
        /// the shortcus for <see cref="Translate"/>
        /// </summary>
        /// <returns></returns>
        public static string M(this string key, string defaultText, CultureInfo culture, params object[] args)
        {
            var text = Translate(key, culture, defaultText);

            return FormatContent(args, text);
        }

        /// <summary>
        /// the shortcus for <see cref="Translate"/> with singural and plural
        /// </summary>
        /// <returns></returns>
        public static string Plural(this string singular, string plural, int count, params object[] args)
        {
            string text = count == 1 ? Translate(singular, Thread.CurrentThread.CurrentUICulture,null) : Translate(plural, Thread.CurrentThread.CurrentUICulture,null);

            return FormatContent(args, text);
        }
       
        /// <summary>
        /// 单复数文本
        /// </summary>
        /// <param name="singular"></param>
        /// <param name="plural"></param>
        /// <param name="count"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Plural(this string singular, string plural, int count, CultureInfo culture, params object[] args)
        {
            string text = count == 1 ? Translate(singular, culture,null) : Translate(plural,culture,null);

            return FormatContent(args, text);
        }

        private static string FormatContent(object[] args, string text)
        {
            if (args != null && args.Length > 0)
            {
                return string.Format(text, args);
            }
            else
            {
                return text;
            }
        }
       


    }
}
